/*The MIT License

Copyright (c) <1/27/2011> <erick.difiore>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/

package com.d4a5.mapping.google
{
	import com.d4a5.mapping.core.AmfFactory;
	import com.d4a5.mapping.core.AmfObject;
	import com.d4a5.mapping.core.Utils;
	import com.google.maps.InfoWindowOptions;
	import com.google.maps.LatLng;
	import com.google.maps.interfaces.IInfoWindow;
	import com.google.maps.interfaces.IMarker;
	import com.google.maps.overlays.MarkerOptions;
	
	import flash.geom.Point;
	import flash.utils.Dictionary;
	import flash.utils.Endian;
	
	public class wmgMarker extends wmgBaseOverlay implements IMarker
	{
		protected var _pt:LatLng;
		protected var _markerOptions:MarkerOptions;
		
		
		public function wmgMarker( geometry:Object , options:MarkerOptions = null )
		{
			super();
			
			this.setOptions( options );
			
			if( geometry is LatLng ){
				this._pt = geometry as LatLng;
				this._amf = AmfFactory.Create( AmfFactory.formatGoogle , AmfFactory.typePoint , this._pt , this.attributes );
			}
			else if( geometry is AmfObject ){
				this._amf = geometry as AmfObject;
				this.attributes = this._amf.attributes;
			}
		}
		
		public function isHidden():Boolean
		{
			return !(this.visible);
		}
		
		public function hide():void
		{
			this.visible = false;
		}
		
		public function openInfoWindow(arg0:InfoWindowOptions=null, useSeperatePane:Boolean=false ):IInfoWindow
		{
			throw new Error( "Error : openInfoWindow not implemented" );
			return null;
		}
		
		public function setOptions(arg0:MarkerOptions):void
		{
			this._markerOptions = arg0;
		}
		
		public function getOptions():MarkerOptions
		{
			var options:MarkerOptions = this.retrieveOptions() as MarkerOptions;
			
			if( this._markerOptions ){
				if( options ){
					return MarkerOptions.merge( [ this._markerOptions , options ] );
				}
				return this._markerOptions;
			}
			else if( options ){
				return options;
			}
			
			return MarkerOptions.getDefaultOptions();
		}
		
		public function getLatLng():LatLng
		{
			return this.pt;
		}
		
		public function show():void
		{
			this.visible = true;
		}
		
		public function closeInfoWindow():void
		{
			throw new Error( "Error : closeInfoWindow not implemented" );
		}
		
		public function setLatLng(arg0:LatLng):void
		{
			this._pt = arg0;
			var storedAsFloat:Boolean = ( this._amf ) ? this._amf.storedAsFloat : false;
			this._amf = AmfFactory.Create( AmfFactory.formatGoogle , AmfFactory.typePoint , this._pt , this.attributes , storedAsFloat );
		}
		
		protected function get pt() : LatLng {
			if( this._pt == null && this._amf ){
				
				this._amf.geometry.position = 0;
				this._amf.geometry.endian = Endian.BIG_ENDIAN;
				
				var readCoord:Function = ( this._amf.storedAsFloat ) ? this._amf.geometry.readFloat : this._amf.geometry.readDouble;
				
				var geographicPt:Point = new Point();
				Utils.WebMercatorToGeographicFaster( readCoord() , readCoord() , geographicPt );
				this._pt = new LatLng( geographicPt.y , geographicPt.x );
			}
			return this._pt;
		}
	}
}